const { build } = require('vite/dist/node/build/index');
const { resolve } = require('path');
const shell = require('shelljs');
const config = require('../vite.config');
const dist = resolve(__dirname, '../dist');
const examples = resolve(__dirname, '../examples');
(async function () {
  console.log('... cleaning ...');
  shell.rm('-rf', dist);
  console.log('... building ...');
  let mode = 'production';
  if (process.env.GH_PAGE) {
    mode = 'gh';
  } else if (process.env.REMOTE) {
    mode = 'remote';
  }
  try {
    await build({
      root: config.root,
      alias: config.alias,
      base: config.base,
      mode,
    });
    shell.cp(
      resolve(__dirname, '../public/dist/index.html'),
      resolve(__dirname, '../public/dist/pvg.html')
    );
    shell.cp(
      resolve(__dirname, '../public/dist/index.html'),
      resolve(__dirname, '../public/dist/ocean.html')
    );

    shell.cp(
      '-R',
      resolve(__dirname, '../public/dist'),
      resolve(__dirname, '..')
    );
    shell.cp(resolve(__dirname, '../.nojekyll'), dist);
    if (mode === 'gh') {
      shell.cp('-R', examples, dist);
    }
    console.log('... 🌟Done🌟 ...');
  } catch (err) {
    console.error(err);
  } finally {
    process.exit(0);
  }
})();
